
package com.jf.cloud.service.sysmanager.remote;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgBaseReasonsService;
import com.jf.cloud.service.sysmanager.entity.PubgBaseReasons;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

import net.sf.json.JSONObject;

/**
 * 员工控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgBaseReasons")
public class PubgBaseReasonsController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBaseReasonsController.class);

	/**
	 * 员工服务
	 */
	@Resource
	private IPubgBaseReasonsService pubgBaseReasonsService;

    @Autowired
    private TokenHandler tokenHandler;
    
	/**
	 * 员工列表数据
	 * 
	 * @author 代码机1.0 创建时间：2018-03-31
	 */
	@RequestMapping(params = "method=data.json")
	public AjaxPageData<PubgBaseReasons> listPubgBaseReasonsData(OPaging<PubgBaseReasons> page, HttpServletRequest request) {
		AjaxPageData<PubgBaseReasons> dataPage = new AjaxPageData<PubgBaseReasons>();
		try {
			dataPage = getAjaxPageData(pubgBaseReasonsService.findPage(page));
		} catch (Exception e) {
			log.error("获取员工列表数据出错！", e);
		}
		return dataPage;
	}

	/**
	 * 员工列表页面
	 * 
	 * @author 代码机1.0 创建时间：2018-03-31
	 */
	@RequestMapping(params = "method=listPubgBaseReasons")
	public String listPubgBaseReasons(HttpServletRequest request) {
		return "/pubgBaseReasonsList";
	}
	
    /**
     * 获取原因管理数据
     * 
     * @author 朱建文 创建时间：2018年4月19日 下午3:57:56
     */
    @ResponseBody
    @RequestMapping("/data.json")
    public AjaxPageData<PubgBaseReasons> getDataList(HttpServletRequest request, OPaging<PubgBaseReasons> xPage){
        AjaxPageData<PubgBaseReasons> pageData = new AjaxPageData<PubgBaseReasons>();
        try {
            String token = request.getParameter("token");
            String sysStaffJson = tokenHandler.getCommUserInfoByToken(token);
            if (sysStaffJson == null || sysStaffJson.trim().length() == 0) {
                return pageData;
            }
            pageData = getAjaxPageData(this.pubgBaseReasonsService.findPage(xPage));
        } catch (Exception e) {
            log.error("获取原因管理数据出错了" , e);
        }
        return pageData;
    }

    /**
     * 添加原因
     * 
     * @author 朱建文 创建时间：2018年4月19日 下午3:58:06
     */
    @ResponseBody
    @RequestMapping("/save")
    public String addBaseContactOffical(HttpServletRequest request, HttpServletResponse response, PubgBaseReasons tradeBaseReasons){
        Result<PubgBaseReasons> result = new Result<PubgBaseReasons>();
        result.setCode(-1);
        result.setMessage("添加联系我们出错了");
        try{
            String token = request.getParameter("token");
            String sysStaffJson = tokenHandler.getCommUserInfoByToken(token);
            if (sysStaffJson == null || sysStaffJson.trim().length() == 0) {
                result.setCode(-2);
                return result.toString();
            }
            if ("1".equals(request.getParameter("isReCommit"))) {
                result.setMessage("请勿重复提交！");
                return result.toString();
            }

            if (Long.valueOf(0).equals(tradeBaseReasons.getRowId())) {
                tradeBaseReasons.setRowId(null);
            }


            JSONObject sysStaff = JSONObject.fromObject(sysStaffJson);
            tradeBaseReasons.setUseFlag(1);
            tradeBaseReasons.setOptStaffId(sysStaff.getLong("rowId"));
            tradeBaseReasons.setOptStaffAccount(sysStaff.getString("staffAccount"));
            tradeBaseReasons.setOptTime(DateUtil.getCurrentDateTime());
            pubgBaseReasonsService.saveObj(tradeBaseReasons);
            result.setCode(0);
        } catch (Exception e) {
            log.error("添加原因管理出错了", e);
            return result.toString();
        }
        return result.toString();
    }

    /**
     * 原因修改页面 获取原因信息
     * 
     * @author 朱建文 创建时间：2018年4月19日 下午3:58:17
     */
    @ResponseBody
    @RequestMapping("/getInfo")
    public PubgBaseReasons getBaseContactOfficalInfo(Long rowId, HttpServletRequest request){
        PubgBaseReasons tradeBaseReasons = new PubgBaseReasons();
        try {
            // 修改焦点图信息时
            if (rowId != null && rowId.longValue() > 0) {
                tradeBaseReasons = this.pubgBaseReasonsService.findByPK(rowId);
            }else{
                tradeBaseReasons.setUseFlag(1);
                tradeBaseReasons.setResponsible(1);
            }
        } catch (Exception e) {
            log.error("原因修改页面 获取原因信息出错！", e);
        }
        return tradeBaseReasons;
    }

    /**
     * 修改原因
     * 
     * @author 朱建文 创建时间：2018年4月19日 下午3:58:35
     */
    @ResponseBody
    @RequestMapping("/update")
    public String updateBaseContactOffical(HttpServletRequest request, HttpServletResponse response, PubgBaseReasons tradeBaseReasons) {
        Result<PubgBaseReasons> result = new Result<PubgBaseReasons>();
        result.setCode(-1);
        result.setMessage("修改原因出错");
        try{
            String token = request.getParameter("token");
            String sysStaffJson = tokenHandler.getCommUserInfoByToken(token);
            if (sysStaffJson == null || sysStaffJson.trim().length() == 0) {
                result.setCode(-2);
                return result.toString();
            }
            if ("1".equals(request.getParameter("isReCommit"))) {
                result.setMessage("请勿重复提交！");
                return result.toString();
            }

            JSONObject sysStaff = JSONObject.fromObject(sysStaffJson);
            tradeBaseReasons.setOptStaffId(sysStaff.getLong("rowId"));
            tradeBaseReasons.setOptStaffAccount(sysStaff.getString("staffAccount"));
            tradeBaseReasons.setOptTime(DateUtil.getCurrentDateTime());
            pubgBaseReasonsService.updatePubgBaseReasons(tradeBaseReasons);
            result.setCode(1);
        } catch (Exception e) {
            log.error("修改原因管理出错了", e);
            return result.toString();
        }
        return result.toString();
    }

    /**
     * 根据责任方获取原因
     * 
     * @author 朱建文 创建时间：2018年1月6日 下午3:30:23
     */
    @ResponseBody
    @RequestMapping("/reasonSelect")
    public List<Map<String,Object>> reasonDictList(Long reasonTypeId, Integer tsbjZrf, HttpServletRequest request){
    	List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
    	try {
			String token = request.getParameter("token");
			String sysStaffJson = tokenHandler.getCommUserInfoByToken(token);
			if (sysStaffJson == null || sysStaffJson.trim().length() == 0) {
				return dataList;
			}
			
			Map<String,String> columns = new HashMap<String,String>();
			columns.put("ROW_ID", "value");
			columns.put("REASON_DES", "label");
			Query query = new Query();
			query.put("REASON_TYPE_ID", reasonTypeId);
			if (tsbjZrf != null && tsbjZrf.intValue() > 0) {
				query.put("RESPONSIBLE", tsbjZrf);
			}
			query.put("USE_FLAG", 1);
			dataList = this.pubgBaseReasonsService.findMapList(columns, query);
		} catch (Exception e) {
			log.error("根据责任方获取原因出错！", e);
		}
    	return dataList;
    }

}
